package com.ebt.m.proposal_v2.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.utils.ActivityUtils;
import com.ebt.m.proposal_v2.base.Constant;
import com.ebt.m.proposal_v2.event.DeleteMoreInsuredEvent;
import com.ebt.m.proposal_v2.helper.ThemeHelper;
import com.ebt.m.proposal_v2.interfaces.OnRxClickListener;
import com.ebt.m.proposal_v2.utils.RxUtils;
import com.ebt.m.proposal_v2.utils.SharePrefrenceUtils;
import com.ebt.m.proposal_v2.widget.base.CompatLinearLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * 模块的头部，如投保人信息、被保人信息、险种信息等
 *
 * @author archer.qi
 *         Created on 2017/5/8.
 */
public class ProposalHeader extends CompatLinearLayout {

    @BindView(R.id.module_header_title)
    TextView headerTitle;
    @BindView(R.id.module_header_import_customer)
    TextView headerImportCustomer;
    @BindView(R.id.module_header_helper)
    ProposalHelper proposalHelper;
    @BindView(R.id.bottom)
    RelativeLayout mHeaderContainer;
    @BindView(R.id.delete_more_insured)
    ImageView delete_more_insured;

    public ProposalHeader(Context context) {
        super(context);
    }

    public ProposalHeader(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.proposalModuleHeader);
        String title = t.getString(R.styleable.proposalModuleHeader_headerTitle);
        setTitle(title);
        boolean hideRight = t.getBoolean(R.styleable.proposalModuleHeader_hideRight, true);
        if (hideRight) {
            hideRight();
        } else {
            showRight();
        }

    }

    private boolean showDelete;
    public void showDelete(boolean showDelete){
        this.showDelete = showDelete;
        updateDeleteViewState();
    }

    private void updateDeleteViewState(){
        if (delete_more_insured != null){
            if (showDelete){
                delete_more_insured.setVisibility(View.VISIBLE);
            } else {
                delete_more_insured.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.proposal_v2_header;
    }

    @Override
    protected void onBindView() {
        delete_more_insured.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new DeleteMoreInsuredEvent(delete_more_insured));
            }
        });
        updateDeleteViewState();
    }

    public void setTitle(String title) {
        headerTitle.setText(title);
    }

    public void setTitle(int resId) {
        setTitle(getString(resId));
    }

    public void hideRight() {
        headerImportCustomer.setVisibility(GONE);
    }

    public void showRight() {
        headerImportCustomer.setVisibility(VISIBLE);
    }

    /**
     * 用于设置导入客户是否可操作
     *
     * @param enabled
     */
    public void setImportEnabled(boolean enabled) {
        headerImportCustomer.setVisibility(enabled ? VISIBLE : GONE);
    }

    /**
     * 投保人Tip
     *
     * @param isApplicant
     */
    public void setHelperView(boolean isApplicant) {
        if (isApplicant) {
            boolean hasHelper = SharePrefrenceUtils.getDefault().getBoolean(Constant.SP_PROPOSAL_APPLICANT_TIP, true);
            if (hasHelper) {
                double density = ActivityUtils.getScreenDensity(getContext());
                proposalHelper.setVisibility(VISIBLE);
                proposalHelper.setDirectRightMargin((int) (170 * density / 3.0));
                proposalHelper.setMessage(ThemeHelper.getString(getViewContext(), R.string.applicant_tip));

                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mHeaderContainer.getLayoutParams();

                double offset = 45 * 3.0 / density + (3.0 - density) * 30;
                params.topMargin = (int) (166 - offset);
                mHeaderContainer.setLayoutParams(params);

                proposalHelper.onDeleteListener(v -> {
                    SharePrefrenceUtils.getDefault().putBoolean(Constant.SP_PROPOSAL_APPLICANT_TIP, false);
                    proposalHelper.setVisibility(GONE);
                    FrameLayout.LayoutParams params1 = (FrameLayout.LayoutParams) mHeaderContainer.getLayoutParams();
                    params1.topMargin = 0;
                    mHeaderContainer.setLayoutParams(params1);
                });
                /* 改为第一次创建计划书显示，之后便不再显示；由此显示之后，设为隐藏*/
                SharePrefrenceUtils.getDefault().putBoolean(Constant.SP_PROPOSAL_APPLICANT_TIP, false);
            } else {
                proposalHelper.setVisibility(GONE);
            }
        }
    }

    public void hideHelperView() {
        proposalHelper.setVisibility(GONE);
        FrameLayout.LayoutParams params1 = (FrameLayout.LayoutParams) mHeaderContainer.getLayoutParams();
        params1.topMargin = 0;
        mHeaderContainer.setLayoutParams(params1);
    }

    // 导入客户监听
    public void addImportCustomerListener(OnRxClickListener listener) {
        RxUtils.click(headerImportCustomer, listener);
    }


}
